package com.ljtg.ljtgservice.controller.admin;

import com.ljtg.context.BaseContext;
import com.ljtg.ljtgpojo.dto.CategoryPageQueryDTO;
import com.ljtg.ljtgpojo.dto.CommodityDTO;
import com.ljtg.ljtgpojo.dto.CommodityPageDTO;
import com.ljtg.ljtgpojo.entity.Commodity;
import com.ljtg.ljtgpojo.vo.CommodityVO;
import com.ljtg.ljtgservice.service.CommodityService;
import com.ljtg.result.PageResult;
import com.ljtg.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/commodity")
@Tag(name = "商品管理接口", description = "商品管理接口")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @PostMapping
    @Operation(summary = "添加商品", description = "添加一个新的商品")
    public Result addCommodity(@RequestBody CommodityDTO commodityDTO) {
        commodityService.addCommodity(commodityDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取商品", description = "根据ID获取商品信息")
    public Result<CommodityVO> getCommodityById(@PathVariable Long id) {
        CommodityVO commodityVO=commodityService.getCommodityById(id);
        return Result.success(commodityVO);
    }

    //根据分类id查询商品
    @GetMapping("/list")
    public Result<List<Commodity>> list(Long categoryId){
        List<Commodity> list = commodityService.list(categoryId);
        return Result.success(list);
    }

    @PutMapping
    @Operation(summary = "更新商品", description = "更新商品信息")
    public Result updateCommodity(@RequestBody CommodityDTO commodityDTO) {
        commodityService.updateCommodity(commodityDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除商品", description = "根据ID删除商品")
    public Result deleteCommodityById(@PathVariable List<Long> ids) {
        commodityService.deleteCommodityById(ids);
        return Result.success();
    }

    //分类查询
    @GetMapping("/page")
    @Operation(summary = "分页查询商品", description = "根据条件分页查询商品")
    public Result<PageResult> page(CommodityPageDTO commodityPageDTO) {
        PageResult pageResult = commodityService.pageQuery(commodityPageDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/get")
    public Long save1(){
        Long currentId = BaseContext.getCurrentId();
        return currentId;
    }
}